<?php
/*index 页面测试*/

namespace app\member\controller;
use app\member\controller\db\ClassList;
use app\member\controller\db\Image;
use app\member\controller\db\Like;
use app\member\controller\db\Member;
use app\member\model\Image as ImageModel;

use think\facade\Session;


class Index extends CheckLoginBase
{

    /*相当于main方法*/
    public function index(){
        $member = new Member();
        /*-----------------------动态查询用户信息------------------------*/
        $member = $member->selectByName(Session::get("login"));
        $image = new Image();
        $like = new \app\member\controller\db\Like();
        $classes = new ClassList();
        $images = $image->selectAll();//---//
        $this->assign("information",$member)
            ->assign("images",$images)
            ->assign("imageCount",$images->total())
            ->assign("likes",$like->selectImageByAuthor(Session::get("login")))
            ->assign("thisClass","今日热图")
            ->assign('title','PhotoShare|首页')
            ->assign("classes" ,$classes->selectAllClass())
            ->assign("imageFirst",$image->selectFirst());
        return $this->fetch('index');
    }

    /*通过图片MD5查询显示详情页*/
    public function showDetails($md5){
        $member = new Member();
        $classes = new ClassList();
        $like = new \app\member\controller\db\Like();
        $image = new Image();
        $details =  $image->selectByMd5($md5);
        $images =  $image->selectByAuthorMain($details['image_member_name']);
        /*-----------------------动态查询用户信息------------------------*/

        $data = $member->selectByName(Session::get("login"));
        $this->assign("information", $data)
            ->assign("details",$details)
            ->assign("reviews",$image->reviewMsg($md5))
            ->assign("imageCount",$images->total())
            ->assign('title','PhotoShare|图片详情')
            ->assign("likes",$like->selectImageByAuthor(Session::get("login")))
            ->assign("thisClass","今日热图")
            ->assign("classes",$classes->selectAllClass())
            ->assign("images", $images);
        return $this->fetch("details");
    }




    public function showSearch(){
        $member = new Member();
        /*-----------------------动态查询用户信息------------------------*/
        if(filter_var(Session::get("login"), FILTER_VALIDATE_EMAIL)){
            $data = $member->selectByEmail(Session::get("login"));
        }else{
            $data = $member->selectByName(Session::get("login"));
        }
        $this->assign("information", $data);
        $this->assign('title','PhotoShare|搜索结果');
        return $this->fetch("search");
    }


    /*相当于main方法*/
    public function indexByClass($className){
        $member = new Member();
        /*-----------------------动态查询用户信息------------------------*/
        $data = $member->selectByName(Session::get("login"));
        $image = new Image();
        $like = new Like();
        $classes = new ClassList();
        $images = $image->selectByClass($className);
        $this->assign("information",$data)
            ->assign("images",$images)
            ->assign("imageCount",$images->total())
            ->assign("likes",$like->selectImageByAuthor(Session::get("login")))
            ->assign("thisClass",$className)
            ->assign('title','PhotoShare|首页')
            ->assign("classes" ,$classes->selectAllClass())
            ->assign("imageFirst",$image->selectFirst());
        return $this->fetch('index');
    }



    public function exitLogin(){
        Session::delete("login");
        $this->redirect("/");
    }

    public function showPersonal(){
        $member = new Member();
        /*-----------------------动态查询用户信息------------------------*/
        if(filter_var(Session::get("login"), FILTER_VALIDATE_EMAIL)){
            $data = $member-> selectByEmail(Session::get("login"));
        }else{
            $data = $member->selectByName(Session::get("login"));
        }
        $this->assign("information", $data);
        $this->assign('title','PhotoShare|个人中心');
        return $this->fetch("index/personal/personal");
    }



    public function showUpload(){
        $member = new Member();
        $class = new ClassList();
        $images = new Image();
            /*-----------------------动态查询用户信息------------------------*/
        if(filter_var(Session::get("login"), FILTER_VALIDATE_EMAIL)){
            $data = $member-> selectByEmail(Session::get("login"));
        }else{
            $data = $member->selectByName(Session::get("login"));
        }
        $this->assign("information", $data)
            ->assign("classes",$class->selectAllClass())
            ->assign("images",$images->selectByAuthor(Session::get("login")))
            ->assign("deleted",$images->deletedImage(Session::get("login")))
            ->assign('title','PhotoShare|我的上传');
        return $this->fetch("upload");
    }

    public function showVip(){
        $member = new Member();
        /*-----------------------动态查询用户信息------------------------*/
        if(filter_var(Session::get("login"), FILTER_VALIDATE_EMAIL)){
            $data = $member-> selectByEmail(Session::get("login"));
        }else{
            $data = $member->selectByName(Session::get("login"));
        }
        $this->assign("information", $data);
        $this->assign('title','PhotoShare|vip');
        return $this->fetch("index/personal/personal_vip");
    }

    public function showCollect(){
        $member = new Member();
        /*-----------------------动态查询用户信息------------------------*/
        if(filter_var(Session::get("login"), FILTER_VALIDATE_EMAIL)){
            $data = $member-> selectByEmail(Session::get("login"));
        }else{
            $data = $member->selectByName(Session::get("login"));
        }
        $this->assign("information", $data);
        $this->assign('title','PhotoShare|我的收藏');
        return $this->fetch("index/personal/personal_collect");
    }

    public function showAttention(){
        $member = new Member();
        /*-----------------------动态查询用户信息------------------------*/
        if(filter_var(Session::get("login"), FILTER_VALIDATE_EMAIL)){
            $data = $member-> selectByEmail(Session::get("login"));
        }else{
            $data = $member->selectByName(Session::get("login"));
        }
        $this->assign("information", $data);
        $this->assign('title','PhotoShare|我的关注');
        return $this->fetch("index/personal/personal_attention");
    }


    public function showHistory(){
        $member = new Member();
        /*-----------------------动态查询用户信息------------------------*/
        if(filter_var(Session::get("login"), FILTER_VALIDATE_EMAIL)){
            $data = $member-> selectByEmail(Session::get("login"));
        }else{
            $data = $member->selectByName(Session::get("login"));
        }
        $this->assign("information", $data);
        $this->assign('title','PhotoShare|历史记录');
        return $this->fetch("index/personal/personal_history");
    }





    /*软删除*/
    public function deleteImage($md5){
        $image = new Image();
        return $image->softDeleteImage($md5);
    }
    /*软删除恢复*/
    public  function recoverImage($md5){
        $image = new Image();
        return $image->softRecoverImage($md5);
    }
    /*展示图片*/
    public function showImage($md5){
        $image = new Image();
        $image->showImage($md5);
        $this->redirect("/user/upload");
    }
    /*私有图片*/
    public function hiddenImage($md5){
        $image = new Image();
        $image->hiddenImage($md5);
        $this->redirect("/user/upload");
    }


    /*点赞图片 session*/
    public function likeIt(){
        if($this->request->isAjax()){
            $image = new Image();
            return $image->likeIt($this->request->param("id"));
        }
        return "false";
    }

    public function reviewThis($md5){
        if($this->request->isAjax()){
            $review = new Image();
            return $review->reviewThis($md5,$this->request->param("msg"));
//            return $this->request->param("msg");
        }
        return "false";
    }


}